package chapter1.dataabstraction.exercise;

import edu.princeton.cs.algs4.StdDraw;


/**
 * @author quanlinlin on 2018/11/24 12:24.
 * @version 1.0
 */
public class FibPic {

    public static void main(String[] args) {
        drawFibCircle(20);
        //StdDraw.arc(0,0,0.5,0,Math.PI);
    }

    private static void drawFibCircle(int n) {
        StdDraw.setPenRadius();
        StdDraw.setPenColor();
        int scale = (int) (fib(n)*1.3);
        StdDraw.setXscale(-scale, scale);
        StdDraw.setYscale(-scale, scale);
        double tempX = 0d;
        double tempY = 0d;
        double centerX = 0d;
        double centerY = 0d;
        //double acientX = 0d;
        //double acientY = 0d;

        for (int i = 1; i <= n; i++) {

            long radius = fib(i);   //斐波那契圆弧半径
            System.out.println(radius);
            if (i == 1) {
                StdDraw.arc(centerX, centerY, radius, 0, 90);
            } else {
                int j = i % 4;
                switch (j) {
                    //case 0centerY = radius + tempY;
                    case 0:
                        centerY = radius + tempY;
                        break;
                    case 1:
                        centerX = tempX - radius;
                        break;
                    case 2:
                        centerY = tempY - radius;
                        break;
                    case 3:
                        centerX = tempX + radius;
                }
                StdDraw.arc(centerX, centerY, radius, (i - 1) * 90, i * 90);
            }
            StdDraw.line(tempX, tempY, centerX, centerY);
            if (i % 2 == 0) {
                tempY = centerY;
                //tempX = Math.cos(i * Math.PI / 2) * radius + (i % 4 == 2 ? -centerX : centerX);
                //double v = Math.cos(i * Math.PI / 2) * radius;
                tempX = centerX + (i % 4 == 2 ? -radius : +radius);

            } else {
                tempX = centerX;
                //tempY = Math.sin(i * Math.PI / 2) * radius + (i % 4 == 1 ? centerY : -centerY);
                double v = Math.sin(i * Math.PI / 2) * radius;
                tempY = centerY + (i % 4 == 1 ? +radius : -radius);
            }
            StdDraw.line(tempX, tempY, centerX, centerY);
        }
    }

    private static long fib(int n) {
        if (n == 1) {
            return 1;
        } else if (n == 2) {
            return 1;
        } else {
            return fib(n - 1) + fib(n - 2);
        }
    }
}
